/*  
 * cafe-toolsbar.js
 * @Author :      GUORUI
 * @CreateDate :  2017年8月22日
 * Copyright (c) 2017, NRIET.　
 * 辅助工具条控件
 */
;
define(function() {
    //创建主界面
    function bulidHtml(cfg) {
        var $warp = $('<div>').addClass('cafe-busis-toolsbar');
        if (cfg.barList.menubar) {
            var $l = $("<div>").addClass('cafe-busis-toolsbar-left');
            var $u = $('<ul>');
            $.each(cfg.barList.menubar, function(i, o) {
            	if (o.isHide) return;
                var $btn = $('<li>').attr("id", o.id).addClass(o.icon).text(o.name);
                $u.append($btn);
            })
            $l.append($u);
        }
        if (cfg.barList.toolsbar) {
            var $r = $("<div>").addClass('cafe-busis-toolsbar-right');
            var $u = $('<ul>');
            $.each(cfg.barList.toolsbar, function(i, o) {
                var $btn = $('<li>').attr("id", o.id).addClass(o.icon).text(o.name);
                if (o.isHide) return;
                if (o.isDef) {
                	$btn.addClass("active");
                }
                switch(o.id) {
                	case "location": 
                		var $g = $('<div>').addClass("cafe-at-location");
	                    $.each([
	                        { id: "csj", isDef: true, text: "长三角" },
	                        { id: "china", isDef: false, text: "中国" },
	                        { id: "dy", isDef: false, text: "东亚" }
	                    ], function() {
	                        var $gspan = $('<span>').attr("gid", this.id).text(this.text);
	                        if (this.isDef) {
	                            $gspan.addClass("active");
	                        }
	                        $g.append($gspan);
	                    });
	                    $btn.append($g.hide());
	                    break;
                	case "station": 
                		var $g = $('<div>').addClass("cafe-at-station");
	                    $.each([
	                        { id: "0", isDef: true, text: "基本站" },
	                        { id: "1", isDef: false, text: "区域站" }
	                    ], function() {
	                        var $gspan = $('<span>').addClass("cas-station").attr("type", this.id).text(this.text);
	                        if (this.isDef) {
	                            $gspan.addClass("active");
	                        }
	                        $g.append($gspan);
	                    });
	                    $btn.append($g.hide());
	                    break;
                	case "gistools": 
                		var $g = $('<div>').addClass("cafe-at-gistools");
	                    $.each([
	                        { id: "enlarge", isDef: false, text: "放大" },
	                        { id: "narrow", isDef: false, text: "缩小" },
	                        { id: "distance", isDef: false, text: "测距" },
	                        { id: "area", isDef: false, text: "测面积" },
	                        { id: "changemap", isDef: false, text: "地图切换" }
	                    ], function() {
	                        var $gspan = $('<span>').addClass(this.id).attr("gid", this.id).text(this.text);
	                        $g.append($gspan);
	                    });
	                    $btn.append($g.hide());
	                    break;
                }
                $u.append($btn);
            })
            $r.append($u);
        }
        $warp.append($l).append($r)
        return $warp;
    }
    /**
     * 按钮点击处理
     */
    function btnHandle($that, cfg) {
        switch ($that.attr("id")) {
        	case "location": //快速定位
            case "gistools":
            case "station": //站点选择
                if ($that.hasClass("active")) {
                    $that.find("div").show();
                } else {
                    $that.find("div").hide();
                }
                break;
            case "layer":
            	if ($that.hasClass('active'))
                	$('#c-b-layermanage').show();
                else
                	$('#c-b-layermanage').hide();
            	break;
            case "jcyb": //监测预报
            case "zdfx": //诊断分析
            case "typhoon": //台风路径
            case "warnset": // 报警设置
            case "tqxx": //天气形势
            case "skgc": //实况观测
            case "dlcp": //天气形势
            case "szyb": //数值预报
            case "grid-dl": //智能网格-短临
            case "grid-dq": //智能网格-短期
            case "intime": // 到报率
            case "draw": // 落区绘制
                if ($.isFunction(cfg.call)) {
            		cfg.call($that.attr("id"), $that.hasClass("active"));
                }
                break;
        }
    }
    /**
     * 绑定事件
     */
    function _eves($drop, cfg) {
        $drop.off().on("click", ".cafe-busis-toolsbar-left li", function() {
            if (!$(this).hasClass("disabled")) {
                if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
                } else {
                    $(this).addClass("active").siblings().removeClass('active');
                }
                btnHandle($(this), cfg);
            }
        }).on("click", ".cafe-busis-toolsbar-right li", function() {
            if (!$(this).hasClass("disabled")) {
                if ($(this).hasClass("active")) {
                    $(this).removeClass("active");
                } else {
                    $(this).addClass("active");
                }
                btnHandle($(this), cfg);
            }
        }).on("click", ".cafe-at-location>span", function(e) { //快速定位
            e.stopPropagation();
            $(this).addClass("active").siblings().removeClass('active');
            var type = $(this).attr("gid");
            switch(type) {
	            case "csj": //长三角
	            	GIS.Extent.Set(cfg.gisId, {xmin: 115.72228605734473, xmax: 126.2810022926054, ymax: 33.96759399788137, ymin: 28.809221170446705});
	                break;
	            case "china": //中国
	            	GIS.Extent.Set(cfg.gisId, {xmin: 135, xmax: 80, ymax: 55, ymin: 15});
	                break;
	            case "dy": //东亚
	            	GIS.Extent.Set(cfg.gisId, {xmin: 70, xmax: 166.734, ymax: 51.7746, ymin: 10});
	    			break;
            }
            
        }).on("click", ".cafe-at-gistools>span", function(e) { //gis工具
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            var type = $(this).attr("gid");
            switch(type) {
	            case "enlarge": //放大
	            	$(this).removeClass("active");
	                GIS.Extent.Zoom(cfg.gisId, true);
	                break;
	            case "narrow": //缩小
	            	$(this).removeClass("active");
	                GIS.Extent.Zoom(cfg.gisId, false);
	                break;
	            case "distance"://距离
	    			GIS.Measure({
	    				id: "at-gis",
	    				type: "distance",
	    				status: $(this).hasClass("active")?"start":"end",
	    				GLMap: mapObj[cfg.gisId]
	    			});
	    			break;
	    		case "area"://面积
	    			GIS.Measure({
	    				id:"at-gis",
	    				type: "area",
	    				status: $(this).hasClass("active")?"start":"end",
	    				GLMap: mapObj[cfg.gisId]
	    			});
	    			break;
	    		case "changemap"://切换地图
	    			if ($(this).hasClass('active'))
	    				GIS.Common.changeBaseLayer(cfg.gisId, 'online_WGS84', null);
	    			else
	    				GIS.Common.changeBaseLayer(cfg.gisId, 'quanguo_WGS84', null);
	    			break;
            }
        }).on("click", ".cas-station", function(e) { //站点选择
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            var type = $(this).attr("type");
            var isSel = $(this).hasClass("active");
            GIS.Station.eleShowControl(cfg.gisId,[type], ['WIND', 'VIS', 'TMP', 'TP', 'RH', 'SP'], isSel ? 'show':'hide');
        }).on("click", ".cafe-at-geography>span", function(e) { //地理信息
            e.stopPropagation();
            if ($(this).hasClass("active")) {
                $(this).removeClass("active");
            } else {
                $(this).addClass("active");
            }
            if ("demlayer" == $(this).attr("gid")) {
                GIS.LayerControll.showControll(cfg.gisId, "baselayer", !$(this).hasClass("active"));
            } else if ($(this).hasClass("active") && "bianjielayer" == $(this).attr("gid")) {
                $(".cafe-at-geography span[gid='bianjielayer_noStr']").removeClass("active");
                GIS.LayerControll.showControll(cfg.gisId, "bianjielayer_noStr", false);
            } else if ($(this).hasClass("active") && "bianjielayer_noStr" == $(this).attr("gid")) {
                $(".cafe-at-geography span[gid='bianjielayer']").removeClass("active");
                GIS.LayerControll.showControll(cfg.gisId, "bianjielayer", false);
            }
            GIS.LayerControll.showControll(cfg.gisId, $(this).attr("gid"), $(this).hasClass("active"));
        })
    }
    var CFToolsbar = function(settings) {
        var options = {
            appendTo: null, //控件所在容器
            gisId: null, //GIS的ID
            call: null //控件点击事件
        };
        this.config = $.extend({}, options, settings);

        var $container = $(this.config.appendTo);
        this.$drop = bulidHtml(this.config);
        _eves(this.$drop, this.config);

        if (this.config.appendTo) {
            $container.show().prepend(this.$drop);
        }
    }

    CFToolsbar.prototype = {
        disabledBtn: function(ids) {
            this.$drop.find("li").each(function() {
                if (-1 != $.inArray($(this).attr("id"), ids)) {
                    $(this).removeClass("active").addClass("disabled");
                }
            });
        },
        activeBtn: function(ids, isNoSelected) {
            this.$drop.find("li").each(function() {
                if (-1 != $.inArray($(this).attr("id"), ids)) {
                    $(this).removeClass("disabled");
                    if (!isNoSelected) $(this).addClass("active");
                }
            });
        },
        removeActive: function(ids) {
            this.$drop.find("li").each(function() {
                if (-1 != $.inArray($(this).attr("id"), ids)) {
                    $(this).removeClass("active");
                }
            });
        },
        getArea: function() {
            var $area = this.$drop.find(".cafe-at-zoneswitch>span.active");
            return {
                areaCode: $area.attr("areacode"),
                areaId: $area.attr("areaid")
            };
        },
        getUnActiveStationType: function() {
            var st = [];
            this.$drop.find(".cafe-at-station .cas-station").each(function() {
            	if (!$(this).hasClass('active'))
            		st.push($(this).attr("type"));
            });
            return st;
        },
        getStationType: function() {
            var st = [];
            this.$drop.find(".cafe-at-station .cas-station.active").each(function() {
                st.push($(this).attr("type"));
            });
            return st.join(",");
        },
        setStationType: function(type) {
            this.$drop.find(".cafe-at-station .cas-station[type$='" + type + "']").show().siblings(".cas-station:not(span[type$='" + type + "'])").hide();
        },
        setStationSelFun: function(fun) {
            if (!this.config.stationSelFun) {
                this.config.stationSelFun = [];
            }
            this.config.stationSelFun.push(fun);
        },
        setAreaSelFun: function(fun) {
            this.config.areaSelFun = fun;
        },
        hideDemlayer: function() {
            var $demlayer = this.$drop.find(".cafe-at-geography>span[gid='demlayer']");
            if ($demlayer.hasClass("active")) {
                $demlayer.click();
            }
        },
        trigger: function(id) {
            this.$drop.find("#" + id).click();
        }
    };

    CafeJS.CFToolsbar = function(settings) {
        var drop = new CFToolsbar(settings);
        return drop;
    }
})